package com.glory.brepda.activity.lanjian;

import androidx.appcompat.app.AppCompatActivity;
import com.glory.brepda.R;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.EqpInfoAndHoldCodeList;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

public class EQPHoldOnOrOffActivity extends BaseActivity {

    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.EqpType)
    public FreeUI_ClearEditText EqpType;
    @BindView(R.id.btn_pass)
    public RadioButton btn_pass;
    @BindView(R.id.btn_ng)
    public RadioButton btn_ng;
    @BindView(R.id.HoldOff)
    public LinearLayout HoldOff;
//    @BindView(R.id.holdOffCode)
//    public FreeUI_ClearEditText holdOffCode;
    @BindView(R.id.tvOffType)
    TextView tvOffType;
    @BindView(R.id.ivOffArrow)
    ImageView ivOffArrow;
    @BindView(R.id.flOffList)
    FrameLayout flOffList;
    @BindView(R.id.tvOnType)
    TextView tvOnType;
    @BindView(R.id.ivOnArrow)
    ImageView ivOnArrow;
    @BindView(R.id.flOnList)
    FrameLayout flOnList;
    @BindView(R.id.HoldOffReson)
    public FreeUI_ClearEditText HoldOffReson;
    @BindView(R.id.HoldOffBeiZu)
    public FreeUI_ClearEditText HoldOffBeiZu;
    @BindView(R.id.HoldOn)
    public LinearLayout HoldOn;
//    @BindView(R.id.HoldOnCode)
//    public FreeUI_ClearEditText HoldOnCode;
    @BindView(R.id.HoldOnReson)
    public FreeUI_ClearEditText HoldOnReson;
    @BindView(R.id.HoldOnBeiZu)
    public FreeUI_ClearEditText HoldOnBeiZu;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private EqpInfoAndHoldCodeList eqpinfo;
    private FreeUI_GeneralPop codePop,codePop2;

    private ArrayList<String> off = new ArrayList<>();
    private ArrayList<String> on = new ArrayList<>();


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqphold_on_or_off;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        HoldOff.setVisibility(View.GONE);
        HoldOn.setVisibility(View.GONE);

        initEdit(etEqpId, sa -> {

            off.clear();
            on.clear();
            btn_ng.setEnabled(true);
            btn_pass.setEnabled(true);


            FreeUI_EntityCallBack<BaseResponseBean<EqpInfoAndHoldCodeList>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpInfoAndHoldCodeList>>
                    (new TypeToken<BaseResponseBean<EqpInfoAndHoldCodeList>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<EqpInfoAndHoldCodeList>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        eqpinfo =   response.body().getResponse().getBody();
                        EqpType.setText(eqpinfo.getEQUIPMENT().getHOLDSTATE());
                        if(StringUtils.equals(eqpinfo.getEQUIPMENT().getHOLDSTATE(),"On")){
                            HoldOff.setVisibility(View.VISIBLE);
                            HoldOn.setVisibility(View.GONE);
                            btn_pass.setChecked(true);
                            btn_ng.setEnabled(false);
                            if(eqpinfo.getHOLDOFFLIST() != null){
                                for(String item : eqpinfo.getHOLDOFFLIST()){
                                    off.add(item);
                                }
                            }
                        }else if(StringUtils.equals(eqpinfo.getEQUIPMENT().getHOLDSTATE(),"Off")){
                            HoldOff.setVisibility(View.GONE);
                            HoldOn.setVisibility(View.VISIBLE);
                            btn_ng.setChecked(true);
                            btn_pass.setEnabled(false);
                            if(eqpinfo.getHOLDONLIST() != null){
                                for(String item : eqpinfo.getHOLDONLIST()){
                                    on.add(item);
                                }
                            }
                        }
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<EqpInfoAndHoldCodeList>> response) {
                    super.onError(response);
                    loadError(response.getException(), "EQPHOLDRELEASE");

                }
            };
            JsonObject map = new JsonObject();

            map.addProperty("EQUIPMENTID", getText(etEqpId));
            map.addProperty("ACTIONTYPE", "QUERY");
            NetUtils.requestPostNet("IF.EQPHOLDRELEASE", "Common", EQPHoldOnOrOffActivity.this, map, callBack);
        });

        flOffList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (codePop2 != null) {
                    if (codePop2.isShowing()) {
                        codePop2.dismiss();
                    }
                } else {
                    codePop2 = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flOffList.getWidth(),off , ivOffArrow, (ty, i) -> {
                        tvOffType.setText(ty);
//                        Toast.makeText(getApplicationContext(),ty,Toast.LENGTH_SHORT).show();
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivOffArrow);
                codePop2.showAsDropDown(flOffList, 0, 0);
            }
        });

        flOffList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (codePop2 != null) {
                    if (codePop2.isShowing()) {
                        codePop2.dismiss();
                    }
                } else {
                    codePop2 = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flOffList.getWidth(),off , ivOffArrow, (ty, i) -> {
                        tvOffType.setText(ty);
//                        Toast.makeText(getApplicationContext(),ty,Toast.LENGTH_SHORT).show();
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivOffArrow);
                codePop2.showAsDropDown(flOffList, 0, 0);
            }
        });

        flOnList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (codePop != null) {
                    if (codePop.isShowing()) {
                        codePop.dismiss();
                    }
                } else {
                    codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flOnList.getWidth(),on , ivOnArrow, (ty, i) -> {
                        tvOnType.setText(ty);
//                        Toast.makeText(getApplicationContext(),ty,Toast.LENGTH_SHORT).show();
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivOnArrow);
                codePop.showAsDropDown(flOnList, 0, 0);
            }
        });

    }

    @Override
    protected void initData() {

    }

    @OnClick({ R.id.tvBaseSubmit,R.id.btn_pass,R.id.btn_ng})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                submit();
                break;
            case R.id.btn_pass:
                HoldOff.setVisibility(View.VISIBLE);
                HoldOn.setVisibility(View.GONE);
                break;
            case R.id.btn_ng:
                HoldOff.setVisibility(View.GONE);
                HoldOn.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void submit(){

        if(getText(etEqpId).equals("")){
            showToast("请先扫描设备ID", false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    btnClean1();
                    showToast("成功", true);
                }
                etEqpId.requestFocus();
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "EQPHOLDRELEASE");

            }
        };
        JsonObject map = new JsonObject();

        map.addProperty("EQUIPMENTID", getText(etEqpId));
        map.addProperty("ACTIONREASON", getText(EqpType).equals("On")?getText(HoldOffReson):getText(HoldOnReson));
        map.addProperty("ACTIONCOMMENT", getText(EqpType).equals("On")?getText(HoldOffBeiZu):getText(HoldOnBeiZu));
        map.addProperty("ACTIONCODE",getText(EqpType).equals("On")?tvOffType.getText().toString():tvOnType.getText().toString());
        map.addProperty("ACTIONTYPE", getText(EqpType).equals("On")?"RELEASE":"HOLD");
        NetUtils.requestPostNet("IF.EQPHOLDRELEASE", "Common", EQPHoldOnOrOffActivity.this, map, callBack);
    }

    private void btnClean1(){
        etEqpId.setText(null);
        EqpType.setText(null);
        btn_pass.setChecked(false);
        btn_pass.setEnabled(true);
        btn_ng.setChecked(false);
        btn_ng.setEnabled(true);
        on.clear();
        off.clear();
        tvOnType.setText(null);
        tvOffType.setText(null);
        HoldOffReson.setText(null);
        HoldOffBeiZu.setText(null);
        HoldOnReson.setText(null);
        HoldOnBeiZu.setText(null);
        etEqpId.requestFocus();
    }

}